Ext.define('AM.view.department.DepartmentTree', {
    extend: 'AM.default.DefaultTree',
    alias: 'widget.departmenttree',
    
    store: Ext.create('AM.store.department.DepartmentTree'),
    
    initComponent: function(){
    	this.title = department_lang.departmentFrame_companyTreeTitle;
        this.callParent(arguments);
    },
    listeners: {
        itemclick: function(view, record, htmlelement, index, eventObject, object){
            eventObject.stopEvent();
            view.htmlelementClick = htmlelement;
            // 清除已加粗
            treeElm = view.getEl();
            var divs = treeElm.select("div.textbold");
            divs.each(function(elm){
                elm.removeCls("textbold");
            });
            // div加粗显示
            var trElm = Ext.get(htmlelement);
            var div = trElm.select("div");
            if (div.getCount != 0) {
                var divElm = div.item(0);
                divElm.addCls("textbold");
            }
            
            var tabPanel = this.up("panel").down("tabpanel");
            var grids = tabPanel.items.items;
            for (var i = 0; i < grids.length; i++) {
                gridPanel = grids[i];
                // 设置grid可以被刷新等功能
                gridPanel.isPossibleLoad = true;
                gridPanel.flag = true;
                
                gridPanel.searchDepID = record.data.id;
                gridPanel.searchTreepath = record.data.treepath == null || record.data.treepath == null ? record.raw.treepath:record.data.treepath;
                var store = gridPanel.getStore();
                
                // grid加载生效
                store.isLoad = true;
                
                var newParams = {
                    searchDepID: record.data.id
                };
                // 重置翻页属性
                gridPanel.resetPage();
                // 4.0强制更改store参数写法
                var params = store.proxy.extraParams;
                Ext.apply(params, newParams);
                
            }
            tabPanel.getActiveTab().store.load();
			tabPanel.getActiveTab().flag = false;
        },
        viewready: function(){
            this.getView().addListener("afteritemexpand", function(node, index, item, eOpts){
                var htmlelement = this.htmlelementClick;
                if (htmlelement) {
                    treeElm = this.getEl();
                    var divs = treeElm.select("div.textbold");
                    divs.each(function(elm){
                        elm.removeCls("textbold");
                    });
                    // div加粗显示
                    var trElm = Ext.get(htmlelement);
                    var div = trElm.select("div");
                    if (div.getCount != 0) {
                        var divElm = div.item(0);
                        divElm.addCls("textbold");
                    }
                }
            });
            this.getView().addListener("afteritemcollapse", function(node, index, item, eOpts){
                var htmlelement = this.htmlelementClick;
                if (htmlelement) {
                    treeElm = this.getEl();
                    var divs = treeElm.select("div.textbold");
                    divs.each(function(elm){
                        elm.removeCls("textbold");
                    });
                    // div加粗显示
                    var trElm = Ext.get(htmlelement);
                    var div = trElm.select("div");
                    if (div.getCount != 0) {
                        var divElm = div.item(0);
                        divElm.addCls("textbold");
                    }
                }
            });
        }
    }

});
